//支持Enter键登录
document.onkeydown = function (e) {
    if ($(".bac").length === 0) {
        if (!e) e = window.event;
        if ((e.keyCode || e.which) === 13) {
            var obtnLogin = document.getElementById("submit_btn");
            obtnLogin.focus();
        }
    }
};

$(function () {
    //提交表单
    $('#submit_btn').click(function () {
        $('.msg_bg').remove();
        $('.sub_err').remove();
        show_loading();
        if ($('#username').val() === '') {
            show_err_msg('请填写用户名或手机号码！');
            $('#username').focus();
        } else if ($('#password').val() === '') {
            show_err_msg('请填写密码！');
            $('#password').focus();
        } else {
            //ajax提交表单，#login_form为表单的ID。 如：$('#login_form').ajaxSubmit(function(data) { ... });
            var username = $('#username').val();
            var password = $('#password').val();
            var toUrl = $('#toUrl').val();
            if (toUrl === null || toUrl === '' || toUrl === "undefined") {
                toUrl = '/Home/Index';
            }
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey($('#publicKey').html());
            var encryptedPwd = encrypt.encrypt(password);
            $.ajax({
                type: "POST",
                url: "/UserLogin/SubmitLogin",
                data: { username: username, password: encryptedPwd },
                success: function (d) {
                    $(".msg_bg").remove();//移除遮罩
                    if (d.state === "success") {
                        show_msg('登录成功！  正在为您跳转...', toUrl);
                    } else if (d.state === "fail") {
                        //show_err_msg(d.msg);
                        alert(d.msg);
                        console.log(d.msg);
                    } 
                }
            });
            
        }
    });
});